﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Practices.EnterpriseLibrary.Logging;

namespace FriendsNearby.Services.NearbyNotifications
{
    public static class NearbyNotificationMessageBuilder
    {
        public static void BuildNotificationMessage(IList<string> notifications, Action<string> fragmentCallback, bool sendMessageEvenIfIsEmpty)
        {

            int numberOfNotifications = notifications.Count;
            Logger.Write("{0} notifications found", numberOfNotifications.ToString());

            if (numberOfNotifications == 0 && !sendMessageEvenIfIsEmpty)
                return;

            if (numberOfNotifications == 0)
                fragmentCallback("No new nearby friends.");
            else if (numberOfNotifications == 1)
                fragmentCallback(string.Format("{0} is now nearby.", notifications.First()));
            else
            {
                int i = 0;
                while (i < numberOfNotifications - 2)
                {
                    fragmentCallback(string.Format("{0}, ", notifications[i]));
                    i++;
                }
                fragmentCallback(string.Format("{0} and {1} are now nearby.", notifications[i], notifications[i + 1]));
            }
        }

    }
}
